Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registering a custom Predicate for determining non-blocking threads #3847

Closed

Conversation

trustin
Copy link
Contributor

@trustin trustin commented Jul 15, 2024

Related issue: #3833
Motivation:

It is currently not possible to create a non-blocking threads without implementing the reactor.core.scheduler.NonBlocking interface. Some third-party libraries and frameworks don't directly depend on reactor-core, yet they want to mark the threads they manage as non-blocking.

Modifications:

  • Added a new method Schedulers.registerNonBlockingThreadPredicate() so that a user can register their own Predicate that determines whether a given thread is non-blocking or not
    • Also added Schedulers.resetNonBlockingThreadPredicate() so that a user can reset the registrations.
  • Fixed an incorrectly implemented test that doesn't really test anything:
    • SchedulersTest.isInNonBlockingThreadTrue()

Result:

  • A user can now mark their own Thread classes as non-blocking without depending on reactor-core or implementing the NonBlocking marker interface.

…hreads

Related issue: reactor#3833
Motivation:

It is currently not possible to create a non-blocking threads without
implementing the `reactor.core.scheduler.NonBlocking` interface. Some
third-party libraries and frameworks don't directly depend on
`reactor-core`, yet they want to mark the threads they manage as
non-blocking.

Modifications:

- Added a new method `Schedulers.registerNonBlockingThreadPredicate()`
  so that a user can register their own `Predicate` that determines
  whether a given thread is non-blocking or not
- Fixed an incorrectly implemented test that doesn't really test
  anything:
  - `SchedulersTest.isInNonBlockingThreadTrue()`

Result:

- Closes reactor#3833
- A user can now mark their own `Thread` classes as non-blocking without
  depending on `reactor-core` or implementing the `NonBlocking` marker
  interface.
@trustin trustin requested a review from a team as a code owner July 15, 2024 12:51
@pivotal-cla
Copy link

@trustin Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

1 similar comment
@pivotal-cla
Copy link

@trustin Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-cla
Copy link

@trustin Thank you for signing the Contributor License Agreement!

Copy link
Member

@chemicL chemicL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for following up with the PR. I have just one request, but overall it looks neat and will be happy to merge once the reset functionality is added. Also, please notice the build outcome. I expect it to fail due to license headers not being updated. Please run ./gradlew spotlessApply locally.

assertNonBlockingThread(CustomNonBlockingThread::new, true);
} finally {
// Restore the global predicate.
Schedulers.nonBlockingThreadPredicate = Schedulers.DEFAULT_NON_BLOCKING_THREAD_PREDICATE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is possible only because the package is the same. Please provide a reset functionality (back to the default thread -> false) so that end users in their packages can also validate the behaviour of their custom Predicate but also make sure they can validate the behaviour when it's not registered and the test state does not leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Schedulers.resetNonBlockingThreadPredicate() and updated the test.

Comment on lines +394 to +397
future.complete(null);
} catch (Throwable cause) {
future.completeExceptionally(cause);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this pattern, thanks! In other places we set the check using some atomic type, utilize a latch and then combine the await with an assertion. This allows to use the assertions API directly, awesome.

@chemicL chemicL added the type/enhancement A general enhancement label Jul 17, 2024
@chemicL chemicL added this to the 3.6.9 milestone Jul 17, 2024
@chemicL
Copy link
Member

chemicL commented Jul 17, 2024

Just noticed the branch is targeting main. As this is an additive change, I think we could ship this in 3.6.9. Or are you ok to delay this until 3.7.0? Adding functionality in patch brings a bit of an effort to the usages where the actual runtime library might have the API or might not so you need a way to check if you can register the Predicate.

- Added `Schedulers.resetNonBlockingThreadPredicate()`
- Updated `SchedulersTest` to use and test `resetNonBlockingThreadPredicate()`
@trustin
Copy link
Contributor Author

trustin commented Jul 22, 2024

That's a good idea, @chemicL. Let me send another PR that targets 3.6 soon. 🙇

@trustin trustin requested a review from chemicL July 22, 2024 11:32
@trustin
Copy link
Contributor Author

trustin commented Jul 22, 2024

@chemicL, I sent a new pull request for 3.6: #3854

@chemicL
Copy link
Member

chemicL commented Jul 22, 2024

Thanks, closing this one then.

@chemicL chemicL closed this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants